﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using MediaClient;
using OpenSource.UPnP;
using MediaServer.Utilities;
using System.Media;
using WMPLib;

namespace MediaServer.Client
{
    public partial class MediaClientManager : Form
    {
        MediaPlayback media;
        MediaControlPoint disco;
        VirtualDirectory currentDirectory;
        UPnPDevice currentDevice;

        public MediaClientManager()
        {
            InitializeComponent();

            media = new MediaPlayback();
            disco = new MediaControlPoint();
            disco.OnUpdatedDeviceList += this.RefreshDevicesHandler;
            disco.OnUpdatedContentList += this.RefreshContentHandler;
            disco.Start();

            this.devicesList.DataSource = disco.Devices();
        }

        private void RefreshDevices()
        {
            ((CurrencyManager)devicesList.BindingContext[disco.Devices()]).Refresh();
        }

        private void RefreshDevicesHandler()
        {
            if (InvokeRequired)
            {
                this.Invoke((MethodInvoker)delegate
                {
                    this.RefreshDevices();
                });
            }
            else
            {
                this.RefreshDevices();
            }
        }

        private void RefreshContent(IDIDLSerializable[] Results)
        {
            contentList.Items.Clear();
            contentList.Items.AddRange(Results);
            statusText.Text = "Loaded content list.";
        }

        private void RefreshContentHandler(MediaControlPoint sender, IDIDLSerializable[] Results)
        {
            if (InvokeRequired)
            {
                this.Invoke((MethodInvoker)delegate
                {
                    this.RefreshContent(Results);
                });
            }
            else
            {
                this.RefreshContent(Results);
            }
        }

        private void BrowseDevice(UPnPDevice Device)
        {
            disco.BrowseDevice(currentDevice, (uint)offsetNum.Value, (uint)countNum.Value);
        }

        private void BrowseDirectory(UPnPDevice Device, VirtualDirectory Directory)
        {
            statusText.Text = "Loading content list...";
            contentList.Items.Clear();
            currentDirectory = Directory;
            disco.BrowseDevice(Device, Directory, (uint)offsetNum.Value, (uint)countNum.Value);
        }

        private void BrowseFile(VirtualFile File)
        {
            string url = File.GetURL();
            statusText.Text = "Opening " + url;
            if (media.IsDisposed)
            {
                media = new MediaPlayback();
            }
            media.Show();
            media.Play(url);
        }

        private void LoadContent()
        {
            if (devicesList.SelectedIndex > -1)
            {
                contentList.Items.Clear();
                statusText.Text = "Loading list of content from selected device...";
                UPnPDevice device = disco.Devices()[devicesList.SelectedIndex];
                if (disco.IsContentDirectory(device))
                {
                    currentDevice = device;
                    BrowseDevice(currentDevice);
                }
                else
                {
                    currentDevice = null;
                    statusText.Text = "Selected device does not support ContentDirectory service.";
                }
            }
        }

        private void LoadDeepContent()
        {
            if (contentList.SelectedIndex > -1)
            {
                IDIDLSerializable container = (IDIDLSerializable)contentList.Items[contentList.SelectedIndex];
                if (container is VirtualDirectory)
                {
                    BrowseDirectory(currentDevice, (VirtualDirectory)container);
                }
                else if (container is VirtualFile)
                {
                    BrowseFile((VirtualFile)container);
                }
            }
        }

        private void FiltersChanged()
        {
            if(currentDevice != null) {
                if (currentDirectory != null)
                {
                    BrowseDirectory(currentDevice, currentDirectory);
                }
                else
                {
                    BrowseDevice(currentDevice);
                }
            }
        }

        // Event Bindings

        private void devicesList_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadContent();
        }

        private void contentList_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDeepContent();
        }

        private void filterBox_TextChanged(object sender, EventArgs e)
        {
            FiltersChanged();
        }

        private void countNum_ValueChanged(object sender, EventArgs e)
        {
            FiltersChanged();
        }

        private void offsetNum_ValueChanged(object sender, EventArgs e)
        {
            FiltersChanged();
        }
    }
}
